package com.dalei.vds.soap;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.dalei.vdserver.shared.soap.DebugSoap;

import android.content.Context;

public class DebugSoapImpl extends BaseSoap implements DebugSoap {
	
	private String url;

	private HttpTransportSE ht;

	private SoapSerializationEnvelope envelope;
	
	public DebugSoapImpl(String ipAddress, int port, int timeout) {
		// TODO Auto-generated constructor stub
		super(ipAddress, port, timeout);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("http://");
		stringBuffer.append(super.getIpAddress());
		stringBuffer.append(":");
		stringBuffer.append(super.getPort());
		stringBuffer.append("/VDServer/DebugService?wsdl");
		url = stringBuffer.toString();
		ht = new HttpTransportSE(url, super.getTimeout());
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	}
	
	public DebugSoapImpl(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("http://");
		stringBuffer.append(super.getIpAddress());
		stringBuffer.append(":");
		stringBuffer.append(super.getPort());
		stringBuffer.append("/VDServer/DebugService?wsdl");
		url = stringBuffer.toString();
		ht = new HttpTransportSE(url, super.getTimeout());
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	}

	public String debug() {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, "debug");
		envelope.bodyOut = request;
		String msg = null;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			msg = result.getProperty("return").toString();
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return msg;
	}

	public String getHostName() {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, "getHostName");
		envelope.bodyOut = request;
		String hostName = null;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			hostName = result.getProperty("return").toString();
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return hostName;
	}

	public String getIpAddress() {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, "getIpAddress");
		envelope.bodyOut = request;
		String ipAddress = null;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			ipAddress = result.getProperty("return").toString();
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return ipAddress;
	}

}
